package test.test1售票;

import java.util.concurrent.locks.ReentrantLock;

public class Counter2 extends Thread {
    //第一种方式实现多线程，测试类中MyThread会创建多次，所以需要加static
    static int ticket = 1000;
    ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        //1.循环
        while (true) {
            //2.同步代码块
            try {
                lock.lock();
                //3.判断共享数据（已经到末尾）
                if (ticket == 0) {
                    break;
                } else {
                    //4.判断共享数据（没有到末尾）
                    Thread.sleep(100);
                    ticket--;
                    System.out.println(getName() + "在卖票，还剩下" + ticket + "张票!!!");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }

        }
    }

}
